#include "frmnetsetting.h"
#include "ui_frmnetsetting.h"
#include <qvalidator.h>
#include <QDebug>
#include <QHostAddress>


frmnetsetting::frmnetsetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::frmnetsetting)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_ShowModal, true);
    initController();
}

frmnetsetting::~frmnetsetting()
{
    delete ui;
}

///
/// \brief frmnetsetting::connectToController
///连接控制器
void frmnetsetting::connectToController()
{

}

void frmnetsetting::closeEvent(QCloseEvent *event)
{
    //on_btNetDisconnect_clicked();
}

void frmnetsetting::initController()
{
    setWindowFlags(this->windowFlags()&~Qt::WindowMaximizeButtonHint);//隐藏最大化按钮
    setWindowFlags(this->windowFlags()&~Qt::WindowMinimizeButtonHint);//隐藏最大化按钮

    ui->edIP1->setValidator(new QIntValidator(0, 255, this));
    ui->edIP2->setValidator(new QIntValidator(0, 255, this));
    ui->edIP3->setValidator(new QIntValidator(0, 255, this));
    ui->edIP4->setValidator(new QIntValidator(0, 255, this));
}

void frmnetsetting::on_btNetConnect_clicked()
{
    ///
    ///
    ///控制器1
    QString strIP = ui->edIP1->text() + "." + ui->edIP2->text() + "." + ui->edIP3->text() + "." + ui->edIP4->text();
    QHostAddress* ip = new QHostAddress();
    ip->setAddress(strIP);
    QString port = ui->edPort->text();
    int iPort = port.toInt();

    ///控制器2
    QString strIP0 = ui->edIP1_2->text() + "." + ui->edIP2_2->text() + "." + ui->edIP3_2->text() + "." + ui->edIP4_2->text();
    QHostAddress* ip0 = new QHostAddress();
    ip0->setAddress(strIP0);
    QString port0 = ui->edPort_2->text();
    int iPort0 = port0.toInt();


//    QTcpSocketHelper::getInstance()->connect(ip, iPort);
//    QTcpSocketHelper::getInstance()->connect0(ip0, iPort0);

    ui->ll_tcp_state_2->setText("正在连接服务器。。。");
    ui->btNetConnect->setEnabled(false);

}

void frmnetsetting::on_btNetDisconnect_clicked()
{
    ui->btNetConnect->setEnabled(true);
    ui->ll_tcp_state->setText("断开连接！");
    ui->ll_tcp_state_2->setText("断开连接！");
}

void frmnetsetting::onHandleConnect(int conRes)
{
    if(conRes == 0){
        ui->ll_tcp_state->setText("正在连接服务器。。。");
    }
    else if(conRes == 1)
    {
        ui->ll_tcp_state->setText("连接成功！");
        ui->btNetDisconnect->setEnabled(true);
    }
    else if(conRes == 2)
    {
        ui->ll_tcp_state->setText("连接失败！");
        ui->btNetConnect->setEnabled(true);

    }
}

void frmnetsetting::onHandleConnect0(int conRes)
{
    if(conRes == 0){
        ui->ll_tcp_state_2->setText("正在连接服务器。。。");
    }
    else if(conRes == 1)
    {
        ui->ll_tcp_state_2->setText("连接成功！");
        ui->btNetDisconnect->setEnabled(true);
    }
    else if(conRes == 2)
    {
        ui->ll_tcp_state_2->setText("连接失败！");
        ui->btNetConnect->setEnabled(true);
    }
}

void frmnetsetting::onConnectError(QAbstractSocket::SocketError e)
{
    qDebug() << "设置状态" ;
    ui->ll_tcp_state->setText("连接失败, 请重新连接！");
    ui->btNetConnect->setEnabled(true);
}

void frmnetsetting::onConnectError0(QAbstractSocket::SocketError e)
{
    ui->ll_tcp_state->setText("连接失败, 请重新连接！");
    ui->btNetConnect->setEnabled(true);
}

void frmnetsetting::onState(QAbstractSocket::SocketState e)
{
    if(e == QAbstractSocket::ConnectedState)
    {
        ui->ll_tcp_state->setText("连接成功！");
    }
    else if(e == QAbstractSocket::UnconnectedState)
    {
        ui->ll_tcp_state->setText("连接断开！");
    }
}

void frmnetsetting::onState0(QAbstractSocket::SocketState e)
{
    if(e == QAbstractSocket::ConnectedState)
    {
        ui->ll_tcp_state_2->setText("连接成功！");
    }
    else if(e == QAbstractSocket::UnconnectedState)
    {
        ui->ll_tcp_state_2->setText("连接断开！");
    }
}
